﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace VideoCarousel.Control.VlC
{
    public enum MediaState
    {
        NothingSpecial = 0,
        Opening = 1,
        Buffering = 2,
        Playing = 3,
        Paused = 4,
        Stopped = 5,
        Ended = 6,
        Error = 7
    }

    /// <summary>
    /// VLCPlay.xaml 的交互逻辑
    /// </summary>
    public partial class VLCPlayControl : UserControl, System.ComponentModel.INotifyPropertyChanged,IDisposable
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public event EventHandler<MediaState> StateChanged;

        public bool IsLoop { set; get; } = false;

        private MediaState mediaState = MediaState.NothingSpecial;

        private TimeSpan length;
        public TimeSpan Length
        {
            set
            {
                length = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Length"));
            }
            get
            {
                return length;
            }
        }
        public MediaState MediaState
        {
            set
            {
                mediaState = value;
                StateChanged?.Invoke(this, value);
            }
            get => mediaState;
        }
        public TimeSpan CurrentLength
        {
            set => _play.Time = value;
            get
            {
                try
                {
                    return _play.Time;

                }
                catch (System.AccessViolationException )
                {
                    return new TimeSpan();
                }
            }
        }

        public int Volume
        {
            get
            {
                return _play.Volume;
            }
            set
            {
                _play.Volume = value;
            }
        }

        public VLCPlayControl()
        {
            InitializeComponent();
            Volume = 65;
            _play.LengthChanged += _play_LengthChanged;
            _play.StateChanged += _play_StateChanged;
            
        }
        public void Play()
        {
            MediaState = MediaState.Playing;
            _play.Play();
        }

        public void Pause()
        {
            MediaState = MediaState.Paused;

            _play.Pause();
        }

        public void Stop()
        {
            MediaState = MediaState.Stopped;
           
            _play.Stop();
        }
       

        private void _play_StateChanged(object sender, Meta.Vlc.ObjectEventArgs<Meta.Vlc.Interop.Media.MediaState> e)
        {
            Console.WriteLine(e.Value);
            switch (e.Value)
            {
                case Meta.Vlc.Interop.Media.MediaState.NothingSpecial:
                    MediaState = MediaState.NothingSpecial;
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Opening:
                    MediaState = MediaState.Opening;

                    break;
                case Meta.Vlc.Interop.Media.MediaState.Buffering:
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Playing:
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Paused:
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Stopped:
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Ended:
                    MediaState = MediaState.Ended;
                    if (IsLoop)
                    {
                        Task.Factory.StartNew(()=> {
                            this.Dispatcher.Invoke(() =>
                            {
                                Play();
                            });
                        });
                    }
                    break;
                case Meta.Vlc.Interop.Media.MediaState.Error:
                    MediaState = MediaState.Error;
                    break;
                default:
                    break;
            }
        }

        private void _play_LengthChanged(object sender, EventArgs e)
        {
            Length = _play.Length;
        }

        public void Load(string fileName)
        {
            if (_play.State == Meta.Vlc.Interop.Media.MediaState.Playing)
            {
                Stop();
            }
            _play.LoadMedia(fileName);
        }

        public void Dispose()
        {
            _play.Stop();
            _play.Dispose();
        }
    }
}
